package com.yu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yu.entity.User;
import com.yu.entity.UserAddress;
import com.yu.exception.MMallException;
import com.yu.form.UserLoginForm;
import com.yu.form.UserRegisterForm;
import com.yu.result.ResponseEnum;
import com.yu.service.CartService;
import com.yu.service.OrdersService;
import com.yu.service.UserAddressService;
import com.yu.service.UserService;
import com.yu.utils.RegexValidateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-06-20
 */
@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private CartService cartService;
    @Autowired
    private UserAddressService userAddressService;

    /**
     * 用户注册
     * @param userRegisterForm
     * @param bindingResult
     * @return
     */
    @PostMapping("/register")
    //BindingResult用在实体类校验信息返回结果绑定。
    public String register(@Valid  UserRegisterForm userRegisterForm, BindingResult bindingResult){
        //非空校验
        if (bindingResult.hasErrors()){
            log.info("【用户注册】用户信息不能为空");
            throw new MMallException(ResponseEnum.USER_INFO_NULL);
        }

        User register = this.userService.register(userRegisterForm);
        if(register == null){
            log.info("【用户注册】添加用户失败");
            throw new MMallException(ResponseEnum.USER_REGISTER_ERROR);
        }

        return "redirect:/login"; //改为重定向形式
    }

    /**
     * 用户登录
     * @return
     */

    @PostMapping("/login")
    //登录和注册是需要的数据不一样故需要添加一个新的UserLoginForm
    /**
     * @Valid
     * 用于验证注解是否符合要求，直接加在变量user之前，在变量中添加验证信息的要求，
     * 当不符合要求时就会在方法中返回message 的错误提示信息。
     *
     * @Valid 结合 BindingResult实例，Spring将对*被注解的实体*进行约束*校验*
     */
    public String login(@Valid UserLoginForm userLoginForm, BindingResult bindingResult, HttpSession httpSession){
        //非空校验
        if (bindingResult.hasErrors()){
            log.info("【用户登录】用户信息不能为空");
            throw new MMallException(ResponseEnum.USER_INFO_NULL);
        }

        User login = this.userService.login(userLoginForm);
        httpSession.setAttribute("user",login);

        return "redirect:/productCategory/main";
    }

    /**
     * 返回当前用户的订单列表
     * @return
     */
    @GetMapping("/orderList")
    public ModelAndView ordersList(HttpSession session){
        //判断是否为登录用户
        User user = (User) session.getAttribute("user");
        if(user == null){
            log.info("【更新购物车】当前为未登录状态");
            throw new MMallException(ResponseEnum.NOT_LOGIN);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("orderList");
        modelAndView.addObject("orderList", this.ordersService.findAllByUserId(user.getId()));
        modelAndView.addObject("cartList", this.cartService.findVOListByUserId(user.getId()));
        return modelAndView;
    }

    /**
     * 返回当前用户的地址列表
     * @return
     */
    @GetMapping("/addressList")
    public ModelAndView addressList(HttpSession session){
        //判断是否为登录用户
        User user = (User) session.getAttribute("user");
        if(user == null){
            log.info("【更新购物车】当前为未登录状态");
            throw new MMallException(ResponseEnum.NOT_LOGIN);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("userAddressList");

        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user.getId());
        modelAndView.addObject("addressList", this.userAddressService.list(queryWrapper));

        modelAndView.addObject("cartList", this.cartService.findVOListByUserId(user.getId()));
        return modelAndView;
    }

}



















